package com.weight.system.sysoper.web;

import com.weight.system.org.PO.OrgInfo;
import com.weight.system.org.service.OrgInfoService;
import com.weight.system.sysoper.PO.SysOperInfo;
import com.weight.system.sysoper.VO.SysOperInfoVO;
import com.weight.system.sysoper.service.SysOperInfoService;
import com.weight.system.user.PO.UserInfo;
import com.weight.system.user.VO.UserInfoVO;
import com.weight.system.user.service.UserInfoService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 账号信息表
 */
@RestController
@Slf4j
@RequestMapping("/sysOperInfo")
public class SysOperInfoController {

	@Autowired
	private SysOperInfoService sysOperInfoService;

	@Autowired
	private OrgInfoService orgInfoService;
	@Autowired
	private UserInfoService userInfoService;
	private String pattern = "yyyy-MM-dd";
	
	/**
	 * 1.查询账号信息表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForSysOperInfoList",method=RequestMethod.GET)
	public ModelAndView queryForSysOperInfoList(){
			
			Map<Object, Object> map =new HashMap<>();
			map.put("operStatus", 1);
			
			//2、查询账号信息表列表
			List<SysOperInfo> sysOperInfoList = sysOperInfoService.findSysOperInfoByT(map);
			log.info("sysOperInfoList={}",sysOperInfoList);
			List<SysOperInfoVO> sysOperInfoVOList = sysOperInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
		 
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("sysoper/queryForSysOperInfoList");
			modelAndView.addObject("sysOperInfoVOList", sysOperInfoVOList);
			return modelAndView;
	}
	
	/**
	 * 3.添加、修改账号信息表操作
	 * @param operId
	 * @return
	 */
	@RequestMapping(value="/addSysOperInfo")
	public ModelAndView addSysOperInfo(@RequestParam(value="operId",required=false) String operId){
			//1、创建返回对象
			SysOperInfoVO sysOperInfoVO = new SysOperInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(operId)) {
				Map<String, String> map=new HashMap<>();
				map.put("operId", operId);
				log.info("修改操作，查询ID：operId={}",operId);
				SysOperInfo sysOperInfo = sysOperInfoService.findBySysOperInfoId(map);
				this.changePoToVO(sysOperInfoVO,sysOperInfo);
			}
			
			
			//2、查询组织机构信息表列表
			Map<Object, Object> map =new HashMap<>();
			map.put("standbyfield1", "1");
			List<OrgInfo> orgInfoList = orgInfoService.findOrgInfoByT(map);
		 
			//2、查询人员信息表列表
			Map<Object, Object> map2 =new HashMap<>();
			map2.put("userStatus", 1);
			List<UserInfo> userInfoList = userInfoService.findUserInfoByT(map2);
			log.info("userInfoList={}",userInfoList);
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("sysoper/addSysOperInfo");
			modelAndView.addObject("sysOperInfoVO",sysOperInfoVO);
			modelAndView.addObject("orgInfoList",orgInfoList);
			modelAndView.addObject("userInfoList",userInfoList);
			return modelAndView;
	}
	
	/**
	 * 5.保存 添加、修改账号信息表操作
	 * @param sysOperInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveSysOperInfo")
	public JsonResultUtil saveSysOperInfo(@Valid SysOperInfoVO sysOperInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,sysOperInfoVO);
			
			//2.获取id
			String operId=sysOperInfoVO.getOperId();
			SysOperInfo sysOperInfo=new SysOperInfo();
			BeanUtils.copyProperties(sysOperInfoVO,sysOperInfo);
			this.changeVOToPO(sysOperInfoVO,sysOperInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(operId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("operId", operId);
				log.info("修改操作，查询ID：operId={}",operId);
				//查询是否存在
				sysOperInfoService.findBySysOperInfoId(map);
				try{
					sysOperInfo.setUpdateBy("000");
					sysOperInfoService.updateSysOperInfo(sysOperInfo);
					log.info("更新操作成功：sysOperInfo={}",sysOperInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					sysOperInfo.setOperId(CommonUtils.getUUId());
					sysOperInfo.setCreateBy("000");
					sysOperInfo.setUpdateBy("000");
					sysOperInfo.setOperStatus(1);
					sysOperInfoService.insertSysOperInfo(sysOperInfo);
					log.info("添加操作成功：sysOperInfo={}",sysOperInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 2.查询账号信息表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForSysOperInfoListDG",method=RequestMethod.GET)
	public String queryForSysOperInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String operUname = request.getParameter("operUname");
			String operPwd = request.getParameter("operPwd");
			String opeSalt = request.getParameter("opeSalt");
			String userId = request.getParameter("userId");
			String orgId = request.getParameter("orgId");
			String operTel = request.getParameter("operTel");
			String operStatus = request.getParameter("operStatus");
			String operDesc = request.getParameter("operDesc");

			Map<Object, Object> map =new HashMap<>();
			map.put("operUname", operUname);
			map.put("operPwd", operPwd);
			map.put("opeSalt", opeSalt);
			map.put("userId", userId);
			map.put("orgId", orgId);
			map.put("operTel", operTel);
			map.put("operStatus", operStatus);
			map.put("operDesc", operDesc);

			
			//2、查询账号信息表列表
//			List<SysOperInfo> sysOperInfoList = sysOperInfoService.findSysOperInfoByPage(page,rows,map);
			List<SysOperInfo> sysOperInfoList = sysOperInfoService.findSysOperInfoByT(map);
			log.info("sysOperInfoList={}",sysOperInfoList);
			List<SysOperInfoVO> sysOperInfoVOList = sysOperInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<SysOperInfoVO> result = new PageResult<SysOperInfoVO>(sysOperInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param sysOperInfoVO
	 * @param sysOperInfo
	 * @return
	 */
	public void changeVOToPO(SysOperInfoVO sysOperInfoVO,SysOperInfo sysOperInfo){
			BeanUtils.copyProperties(sysOperInfoVO,sysOperInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param sysOperInfoVO
	 * @param sysOperInfo
	 * @return
	 */
	public void changePoToVO(SysOperInfoVO sysOperInfoVO,SysOperInfo sysOperInfo){
			BeanUtils.copyProperties(sysOperInfo,sysOperInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param sysOperInfo
	 * @return
	 */
	public SysOperInfoVO changePoToVO(SysOperInfo sysOperInfo){
			SysOperInfoVO sysOperInfoVO=new SysOperInfoVO();
			BeanUtils.copyProperties(sysOperInfo,sysOperInfoVO);
			

			return sysOperInfoVO;
	}

	/**
	 * 3.查询账号信息表--详情页
	 * @param operId
	 * @return
	 */
	@RequestMapping("/queryForSysOperInfoDetail")
	public ModelAndView queryForSysOperInfoDetail(@RequestParam(value="operId",required=false) String operId){
			//1.校验id
			CommonUtils.validateExistID(operId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("operId", operId);
			log.info("查询账号信息表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			SysOperInfo sysOperInfo = sysOperInfoService.findBySysOperInfoId(map);
			
			SysOperInfoVO sysOperInfoVO = changePoToVO(sysOperInfo);
			//3、返回前台
			log.info("查询账号信息表查询成功：sysOperInfo={}",sysOperInfo);
			modelAndView.setViewName("system/sysoper/queryForSysOperInfoDetail");
			modelAndView.addObject("sysOperInfoVO",sysOperInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	

	
	/**
	 * 6.删除账号信息表操作
	 * @param operId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySysOperInfoId")
	public JsonResultUtil deleteBySysOperInfoId(@RequestParam(value="operId",required=false) String operId){
			//1.校验id
			CommonUtils.validateExistID(operId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("operId", operId);
			sysOperInfoService.findBySysOperInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除账号信息表入参，map={}",map);
			
			try{
				sysOperInfoService.upDelBySysOperInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除账号信息表操作---批量删除
	 * @param operIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySysOperInfoIdBatch")
	public JsonResultUtil deleteBySysOperInfoIdBatch(@RequestParam(value="operIdList",required=false) List operIdList){
			//1.校验id
			CommonUtils.validateExistIDList(operIdList);
			
			//2.校验数据库
			sysOperInfoService.findBySysOperInfoIdList(operIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("operIdList", operIdList);
			map.put("updateBy","000");
			log.info("批量删除账号信息表操作入参，map={}",map);
			
			try{
				sysOperInfoService.upDelBySysOperInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}